using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;

namespace EpisodeWizard.CoreUI
{
	/// <summary>
	/// Summary description for ThreadProgress.
	/// </summary>
	public class FrmThreadProgress : System.Windows.Forms.Form
	{
		public enum EProgressStyle
		{
			Percentage,
			Continuous,
		}

		private System.Windows.Forms.Label labelProgressText1;
		private System.Windows.Forms.ProgressBar progressBar;
		private System.Windows.Forms.Button btnCancel;


		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;
		private Label labelProgressText2;


		/// <summary>
		/// Event called when the user wants to cancel the thread
		/// </summary>
		public delegate void CancelHandler();
		public event CancelHandler RequestCancel;

		/// <summary>
		/// Defines the way we display the thread progress
		/// </summary>
		private EProgressStyle mProgressStyle;
		public EProgressStyle ProgressStyle
		{
			get
			{
				return mProgressStyle;
			}
			set
			{
				mProgressStyle = value;
				if (mProgressStyle == EProgressStyle.Percentage)
				{
					progressBar.Minimum = 0;
					progressBar.Maximum = 100;
					progressBar.Style = ProgressBarStyle.Continuous;
				}
				else
				{
					progressBar.Minimum = 0;
					progressBar.Maximum = 100;
					progressBar.Style = ProgressBarStyle.Marquee;
				}
			}
		}

		public FrmThreadProgress()
		{
			InitializeComponent();
		}


		/// <summary>
		/// Used to be notified when the thread starts
		/// </summary>
		public void OnStart()
		{
		}
	
		/// <summary>
		/// Used to be notified when the thread ends
		/// </summary>
		public void OnEnd()
		{
			Invoke(new Core.ThreadedAction.EndHandler(CloseFromThread));
		}

		public void OnProgress(int Percent, string Text1, string Text2)
		{
			Invoke(new Core.ThreadedAction.ProgressHandler(ProgressFromThread), new object[] { Percent, Text1, Text2 });
		}

		public void CloseFromThread()
		{
			Close();
		}

		public void ProgressFromThread(int Percent, string Text1, string Text2)
		{
			labelProgressText1.Text = Text1;
			labelProgressText2.Text = Text2;

			if (ProgressStyle == EProgressStyle.Percentage)
			{
				progressBar.Value = Percent;
			}
		}

		/// <summary>
		/// Asks the thread to terminate as soon as possible
		/// </summary>
		private void Abort()
		{
			DialogResult Res = MessageBox.Show("Are you sure you want to cancel?", "Confirm cancel", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
			if (Res == DialogResult.Yes)
			{
				if (RequestCancel != null)
				{
					RequestCancel();
				}
			}
		}

		/// <summary>
		/// The user clicked to cancel the thread
		/// </summary>
		private void btnCancel_Click(object sender, System.EventArgs e)
		{
			Abort();
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.labelProgressText1 = new System.Windows.Forms.Label();
			this.progressBar = new System.Windows.Forms.ProgressBar();
			this.btnCancel = new System.Windows.Forms.Button();
			this.labelProgressText2 = new System.Windows.Forms.Label();
			this.SuspendLayout();
			// 
			// labelProgressText1
			// 
			this.labelProgressText1.Location = new System.Drawing.Point(8, 16);
			this.labelProgressText1.Name = "labelProgressText1";
			this.labelProgressText1.Size = new System.Drawing.Size(240, 16);
			this.labelProgressText1.TabIndex = 5;
			this.labelProgressText1.Text = "Progress text";
			// 
			// progressBar
			// 
			this.progressBar.Location = new System.Drawing.Point(8, 40);
			this.progressBar.Name = "progressBar";
			this.progressBar.Size = new System.Drawing.Size(360, 16);
			this.progressBar.TabIndex = 4;
			// 
			// btnCancel
			// 
			this.btnCancel.Location = new System.Drawing.Point(136, 72);
			this.btnCancel.Name = "btnCancel";
			this.btnCancel.Size = new System.Drawing.Size(112, 24);
			this.btnCancel.TabIndex = 3;
			this.btnCancel.Text = "Cancel";
			this.btnCancel.Click += new System.EventHandler(this.btnCancel_Click);
			// 
			// labelProgressText2
			// 
			this.labelProgressText2.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
			this.labelProgressText2.Location = new System.Drawing.Point(270, 16);
			this.labelProgressText2.Name = "labelProgressText2";
			this.labelProgressText2.Size = new System.Drawing.Size(100, 16);
			this.labelProgressText2.TabIndex = 6;
			this.labelProgressText2.Text = "Text 2";
			this.labelProgressText2.TextAlign = System.Drawing.ContentAlignment.TopRight;
			// 
			// FrmThreadProgress
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(376, 111);
			this.ControlBox = false;
			this.Controls.Add(this.labelProgressText2);
			this.Controls.Add(this.labelProgressText1);
			this.Controls.Add(this.progressBar);
			this.Controls.Add(this.btnCancel);
			this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
			this.MaximizeBox = false;
			this.MinimizeBox = false;
			this.Name = "FrmThreadProgress";
			this.ShowInTaskbar = false;
			this.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent;
			this.Text = "Please wait...";
			this.ResumeLayout(false);

		}
		#endregion

	}
}
